"""
示例
"""

from {{cookiecutter.package_name}}.ioc import Bean, Autowired, ConfigurationPropertiesBean, Value
from {{cookiecutter.package_name}} import boot
from pydantic import BaseModel


@Bean('service')
class Service:

    def hello(self):
        print('hello world')


@Bean
class Service2:

    def __init__(self, service: Service):  # 隐式构造函数注入
        # def __init__(self, service = Autowired(cls=Service)): # 显示构造函数注入
        self.service = service

    def hello(self):
        print('hello world2')
        self.service.hello()


@ConfigurationPropertiesBean  # 属性自动装载 Bean 可以添加prop装饰器属性来指定属性域, 默认全局
class ProjectConfig(BaseModel):
    project_name: str
    package_name: str


def main():
    # 启动IOC
    boot.start()

    # IOC 容器注入
    service = Autowired(cls=Service)  # 使用类型注入
    # service = Autowired('service') # 使用名称注入
    service.hello()
    """
    输出:
    hello world
    """

    service2 = Autowired(cls=Service2)
    service2.hello()
    """
    输出:
    hello world2
    hello world
    """

    # 加载config/application的配置
    project_name = Value('${project_name}')
    print(project_name)
    """
    输出:
    '{{cookiecutter.project_name}}'
    """
    print(project_name.str)  # 本质还是一个代理对象, 使用str属性获取字符串类型, 除了str属性, 还有int, float, bool等
    """
    输出:
    {{cookiecutter.project_name}}
    """

    # 注入自动配置 IOC 容器
    project_config = Autowired(cls=ProjectConfig)
    print(project_config.project_name)
    print(project_config.package_name)
    """
    输出:
    {{cookiecutter.project_name}}
    {{cookiecutter.package_name}}
    """


if __name__ == '__main__':
    main()
